#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>

typedef struct student{
	int num;
	float score;
	struct student *next;
}stu;
int n=0;//node count, if n=1, just one node and it is head node.
stu *slink_create()
{
	printf("create init sigle link\n");
	stu *head,*p1,*p2;
	head=NULL;
	p1=(stu *)malloc(sizeof(stu));
	p2=p1;
	printf("please input num(>0) and score(num score):\n");
	scanf("%d %f",&p1->num,&p1->score);
	while(p1->num !=0){
		n=n+1;
		if(n==1)
			head=p1; //just only one node, and this is head node
		else
			p2->next=p1;
		p2=p1;
		p1=(stu *)malloc(sizeof(stu));
		scanf("%d %f",&p1->num,&p1->score);
	}
	p2->next=NULL;
	free(p1);
	p1=NULL;
	return head;
}

void msg_show(stu *head)
{
	printf("show message:\n");
	while(head!=NULL){
		printf("%-4d %5.1f\n",head->num,head->score);
		head=head->next;
	}
}

stu *delnode(stu *head,int key)
{
	printf("will delete node num=%d\n",key);
	stu *p1;//save need check node
	stu *p2;//save checked node
	if(head==NULL) 
		return NULL; //cannot delet null link
	p1=head;
	while(p1->num !=key && p1->next !=NULL){
		p2=p1; //save corrent node address
		p1=p1->next;//move to next node
	}
	if(p1->num==key) //find it
	{
		if(p1==head) //if delete node is the first node
			head=p1->next;//head change to p1 next, so the head node is deletetd
		else
			p2->next=p1->next;
		free(p1);
		p1=NULL;
		printf("delete %d success\n",key);
		n=n-1;//node count reduce
	}else
		printf("note find %d node\n",key);
	return head;
}

stu *insertnode(stu *head, int key, stu *knode)
{
	stu *p1;// save corrent need check node address
	if(head==NULL)
	{
		head=knode;
		knode->next=NULL;
		n=n+1;// node count +1
		return head;
	}
	p1=head;
	while(p1->num !=key && p1->next !=NULL)
		p1=p1->next;//move to next node
	if(key==p1->num) //find it
	{
		knode->next=p1->next;//knode next node is orginal p1's next
		p1->next=knode;// knode insert to p1 behind
		n=n+1;
	}else
		printf("not find %d\n",key);
	return head;
}

int serchnode(stu *head, int key)
{
	stu *p1;
	if(head==NULL)
		return 1;
	head=p1;
	while(p1!=NULL)
	{
		if(p1->num ==key)
		{
			printf("find it:\n");
				printf("%-4d %5.1f\n",p1->num,p1->score);
			return 0;
		}
		p1=p1->next;

	}
	return 0;

}

void free_list(stu *head)
{
	if(head ==NULL)
		return ;
	if(head->next==NULL){
	i	free(head);
		head=NULL;
		return ;
	}
	stu *p=head->next;
	while(p1 !=NULL)
	{
		stu *tmp=p;
		p=p->next;
		free(tmp);
		p2=p1;
	}
	free(head);
	head=NULL;
}
/*
stu modifynode(stu *head,int key, stu *knode)
{
	stu *p1;
	if (head == NULL)
		return NULL;
}
*/
int main(int argc, char **argv){
	stu *head=NULL;
	head=slink_create();
	msg_show(head);
	//memset(&head,0,sizeof(stu *head));
	int del_num=0;
	printf("input delete node(num end of 0):\n");
	scanf("%d",&del_num);
	head=delnode(head,del_num);
	msg_show(head);
	stu *inode;
	inode=(stu *)malloc(sizeof(stu));
	int inum=0;
	printf("please input insert node(num score):\n");
	scanf("%d %f",&inode->num,&inode->score);
	printf("please input insert behind num(end of 0):\n");
	scanf("%d",&inum);
	printf("insert num=%d\n",inum);
	head=insertnode(head,inum,inode);
	msg_show(head);
	free(inode);
	inode=NULL;
	printf("please input the serch num(end of 0):\n");
	int snum=0;
	scanf("%d",&snum);  
	serchnode(head,snum);
	stu *mnode;
	int mnum=0;
	mnode=(stu *)malloc(sizeof(stu));
	printf("please input modify num and modify value( mnum num score):");
	scanf("%d %d %f",&mnun,&mnode->num,&mnode->score);
//	modifynode(mnum,mnode);
	free_list(head);
return 0;
}

//command:
//scanf("%d\n",&inum);
//scanf("%d\n",&snum);
//use \n will fail to input value, can use fflush(stdin) to fix
//or not use \n
